﻿/**
//  Connection.cs
//  Marquee Moment
//  Copyright 2011 Systematix Technocrates. All rights reserved.
**/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using NLog;

namespace MarqueeMoment.Entities    
{
    /// <summary>
    /// Class for DB connection
    /// </summary>
    public class Connection
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        public static string WorkStation = Properties.Settings.Default.WorkstationName;
        public static string UserId = Properties.Settings.Default.UserId;
        public static string Password = Properties.Settings.Default.Password;
        public static string DatabaseName = Properties.Settings.Default.DatabaseName;

        private static List<SqlConnection> connectionArray = new List<SqlConnection>();



        ~Connection()
        {
            if (connectionArray.Count() > 0)
            {
                foreach (SqlConnection conn in connectionArray)
                {

                    if (conn != null)
                    {
                        if (conn.State == ConnectionState.Open)
                        {
                            conn.Close();
                        }


                        conn.Dispose();
                    }
                }
                connectionArray.Clear();
            }

            
        }

        /// <summary>
        /// To open the connection fronm the Db
        /// </summary>
        /// <returns></returns>
        public static SqlConnection GetConnection()
        {
                
            try
            {
                SqlConnection conn = new SqlConnection("server='" + WorkStation + "';uid='" + UserId + "';password='" + Password + "';initial catalog='" + DatabaseName + "'");
                conn.Open();
                connectionArray.Add(conn);
                return conn;
            }
            catch (Exception exc)
            {
                logger.Error(exc.StackTrace);
                Dialogs.Error("Could not connect to Server..\n\n Check workstation value in XML Configuration File for key: WorkstationName");
            }
            return null;
        }
        /// <summary>
        /// For closing the open connection
        /// </summary>
        public static void CloseConnection(SqlConnection conn)
        {
            
            if (conn != null)
            {
                if (connectionArray.Contains(conn))
                    connectionArray.Remove(conn);


                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
                conn.Dispose();
                conn = null;

               
            }		
        }
        /// <summary>
        /// for creating the setting folder to keep the setting information on it.
        /// </summary>
        public static String SettingsFolder
        {
            get
            {
                String settingsPath = System.Windows.Forms.Application.StartupPath + @"\Settings\";

                if (!Directory.Exists(settingsPath))
                    Directory.CreateDirectory(settingsPath);

                return settingsPath;
            }
        }

       
    }
}
